import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    name: '',
    age: 0,
    isLoggedIn: false,
  }),
  actions: {
    login(name, age) {
      this.name = name
      this.age = age
      this.isLoggedIn = true
    },
    logout() {
      this.name = ''
      this.age = 0
      this.isLoggedIn = false
    },
  },
  getters: {
    userInfo(state) {
      return `${state.name}, ${state.age} years old`
    },
  },
  persist: {
    enabled: true, // 启用持久化
    strategies: [
      {
        key: 'user-store', // 存储的键名
        storage: localStorage, // 使用localStorage
      },
    ],
  },
})
